/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 *
 * @author HoangHai
 */
public class StringUtility {

    public static boolean isNull(String str) {
        if (str == null || str.trim().equals("")) {
            return true;
        }
        return false;
    }

    public static String getResourceBundle(String baseName, String key) {
        if (StringUtility.isNull(baseName) || StringUtility.isNull(key)) {
            return "";
        }
        try {
            ResourceBundle resourceBundle = ResourceBundle.getBundle(getLangPath(baseName));
            return resourceBundle.getString(key);
        } catch (Exception ex) {
            return "";
        }
    }

    public static String getErrorMessage(String errCode, String arg0, String arg1, String arg2, String arg3) {
        if (StringUtility.isNull(errCode)) {
            return "";
        }
        try {
            String message = getResourceBundle("message", errCode);
            if (!StringUtility.isNull(arg0)) {
                message = message.replace("{0}", arg0);
            }
            if (!StringUtility.isNull(arg1)) {
                message = message.replace("{1}", arg1);
            }
            if (!StringUtility.isNull(arg2)) {
                message = message.replace("{2}", arg2);
            }
            if (!StringUtility.isNull(arg3)) {
                message = message.replace("{3}", arg3);
            }
            return message;
        } catch (Exception ex) {
            return "";
        }
    }

    public static boolean checkInt(String str) {

        return StringUtility.checkHankakuDigit(str);
    }

    public static boolean checkHankakuDigit(String str) {

        if (!isStr(str)) {
            return false;
        }

        char[] chArray = str.toCharArray();

        for (int count = 0; count < chArray.length; count++) {
            if (!checkHankakuDigit(chArray[count])) {
                return false;
            }
        }
        return true;
    }

    public static boolean isStr(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        return true;
    }

    public static int getByte(String str) {
        if (isNull(str)) {
            return 0;
        }
        return str.getBytes().length;
    }

    public static boolean checkHankakuDigit(char ch) {
        return ch >= 0x0030 && ch <= 0x0039;
    }

    public static String getLangPath(String baseName) {
        return "resource." + baseName;
    }

    public static String encoder(final String sourceStr) {

        AngouManager angouManager = new AngouManager();
        String returnString = null;
        try {
            returnString = angouManager.encrypt(sourceStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnString;
    }

    public static String decrypt(final String sourceStr) {
        AngouManager angouManager = new AngouManager();
        String returnString = null;
        try {
            returnString = angouManager.decrypt(sourceStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnString;
    }

    public static final String encoderByMd5(final String sourceStr) {
        String returnString = null;
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            md5.update(sourceStr.getBytes("utf-8"));
            byte[] tmp = md5.digest();
            char[] str = new char[16 * 2];
            int k = 0;
            for (int i = 0; i < 16; i++) {
                byte byte0 = tmp[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            returnString = new String(str);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return returnString;
    }
}
